package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.RecordAdapter;
import com.wnhz.work.yz.bean.RecordsItemBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class SearchRecordsActivity extends BaseActivity implements View.OnClickListener {
    private EditText et_search;
    private String searchStr = "";
    private RecyclerView search_recycler;
    private String type = "";
    private LinearLayout search_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_records);
        initView();
        initData();

    }


    private void initData() {
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    searchStr = et_search.getText().toString();
                    if (searchStr.equals("")) {
                        search_type.setVisibility(View.VISIBLE);
                        search_recycler.setVisibility(View.GONE);
                    } else {
                        search_type.setVisibility(View.GONE);
                        search_recycler.setVisibility(View.VISIBLE);
                    }
                    upSearch();
                    return true;
                }
                return false;
            }
        });

    }

    private List<RecordsItemBean.InfoEntity> info = new ArrayList<>();

    private void upSearch() {

        Map<String, Object> map = new Hashtable<>();
        //  map.put("type", type);
        map.put("keywords", searchStr);
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.API_RECORDLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result);
                Gson gson = new Gson();
                RecordsItemBean b = gson.fromJson(result, RecordsItemBean.class);
                if (b.getResult().equals("1")) {
                    info = b.getInfo();
                }

            }

            @Override
            public void onFinished() {
                setData();
                super.onFinished();
            }
        });
    }


    private void setData() {

        adapter = new RecordAdapter(this, info);
        search_recycler.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    private LinearLayoutManager linearLayoutManager;
    private RecordAdapter adapter;

    private void initView() {
        search_type = (LinearLayout) findViewById(R.id.search_type);
        findViewById(R.id.cancel).setOnClickListener(this);
        findViewById(R.id.type_0).setOnClickListener(this);
        findViewById(R.id.type_1).setOnClickListener(this);
        et_search = (EditText) findViewById(R.id.et_search);
        search_recycler = (RecyclerView) findViewById(R.id.search_recycler);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        search_recycler.setLayoutManager(linearLayoutManager);
        search_recycler.setHasFixedSize(true);
        search_recycler.setItemAnimator(new DefaultItemAnimator());
        //=========================


        //


    }

    @Override
    public void onClick(View v) {
        searchStr = et_search.getText().toString();
        Intent intent = new Intent(this, OtherRecordSearchActivity.class);

        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.type_0:
                type = "0";
                intent.putExtra("searchStrHint", "线索搜索");
                intent.putExtra("type", type);
                startActivity(intent);
                break;
            case R.id.type_1:
                type = "1";
                //  upSearch();
                intent.putExtra("type", type);
                intent.putExtra("searchStrHint", "精确搜索");
                startActivity(intent);
                break;
        }
    }


}
